package cdu.sl.service.impl;

import cdu.sl.dto.SavePermissionDTO;
import cdu.sl.entity.Permission;
import cdu.sl.mapper.PermissionDao;
import cdu.sl.service.PermissionService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class PermissionServiceImpl implements PermissionService {

    private final PermissionDao permissionDao;

    /**
     * 新增权限
     *
     * @param savePermissionDTO 新增权限信息
     */
    @Override
    public void add(SavePermissionDTO savePermissionDTO) {
        Permission permission = new Permission();
        BeanUtils.copyProperties(savePermissionDTO, permission);
        permissionDao.save(permission);
    }

    /**
     * 删除权限
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        permissionDao.deleteById(id);
    }

    /**
     * 查询所有权限列表
     *
     * @return
     */
    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }

    /**
     * 获取权限信息
     *
     * @param id 权限id
     * @return 权限信息
     */
    @Override
    public Permission getById(Integer id) {
        Optional<Permission> opt = permissionDao.findById(id);
        return opt.orElse(null);
    }

    /**
     * 修改权限
     *
     * @param savePermissionDTO 修改权限信息
     */
    @Override
    public void update(SavePermissionDTO savePermissionDTO) {
        Permission permission = new Permission();
        BeanUtils.copyProperties(savePermissionDTO, permission);
        permissionDao.save(permission);
    }


}
